import sys.io.File;
import sys.FileSystem;

class Client
{
  public static function main()
  {
     var args = Sys.args();

     // When run from "haxelib run ...", the last arg is the original working directory
     var libDir = Sys.getCwd();
     Sys.setCwd( args.pop() );

     var action = args[0];
     if (args.length<2 || (action!="run" && action!="put" && action!="wput") )
     {
        Sys.print("Usage: haxelib run hurts run script.n [\"args\"]\n");
        Sys.print("     : haxelib run hurts put source homeFilename\n");
        Sys.print("     : haxelib run hurts wput source webFilename\n");
        Sys.exit(0);
     }
     if (action!="run" && args.length!=3)
     {
        Sys.print("Missing target filename\n");
        Sys.exit(1);
     }



     var filename = args[1];
     if (action=="run" && !FileSystem.exists(filename))
     {
        var testName = libDir + "/functions/bin/" + filename + ".n";
        if (FileSystem.exists( testName ))
           filename = testName;
     }

     var result = "";

     try
     {
        if (action=="run")
           result = hurts.Lib.runNeko(filename,args.length>2 ? args[2] : "");
        else if (action=="put")
           result = hurts.Lib.sendFile(filename,args[2]);
        else if (action=="wput")
           result = hurts.Lib.sendWebFile(filename,args[2]);

        Sys.println(result);
     }
     catch(e:Dynamic)
     {
        Sys.println("Error : " + e);
        Sys.exit(1);
     }
  }
}

